/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HospedajeNuevo.java
 *
 * Created on 21/10/2010, 01:16:20 AM
 */

package pe.edu.upla.lamerced.desk.view.hospedaje;

import java.util.Date;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.Utiles;
import pe.edu.upla.lamerced.bean.Habitacion;
import pe.edu.upla.lamerced.bean.Hospedaje;
import pe.edu.upla.lamerced.bean.Tipohabitacion;
import pe.edu.upla.lamerced.bean.Users;
import pe.edu.upla.lamerced.desk.view.Login;
import pe.edu.upla.lamerced.service.HabitacionService;
import pe.edu.upla.lamerced.service.HospedajeService;
import pe.edu.upla.lamerced.service.UsuarioService;

/**
 *
 * @author elvis
 */
public class HospedajeReservaNuevo extends javax.swing.JInternalFrame {

   private Hospedaje reserva;
   private Hospedaje hospedaje;
   private JDesktopPane escritorio;
   private HospedajeService hospedajeService;
   private HabitacionService habitacionService;
   private UsuarioService usuarioService;
   private List<Habitacion> listaHabitaciones;

   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }
   public Hospedaje getReserva() {
      return reserva;
   }
   public void setReserva(Hospedaje reserva) {
      this.reserva = reserva;
   }
   public Hospedaje getHospedaje() {
      return hospedaje;
   }
   public void setHospedaje(Hospedaje hospedaje) {
      this.hospedaje = hospedaje;
   }

   /** Creates new form HospedajeNuevo */
    public HospedajeReservaNuevo() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGrabaHospedje = new javax.swing.JButton();
        btnCancelaHospedaje = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblFecha = new javax.swing.JLabel();
        lblCliente = new javax.swing.JLabel();
        lbldiasreserva = new javax.swing.JLabel();
        cbTipoHabitacion = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        cbHabitacion = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtFecha = new javax.swing.JTextField();
        txtdias = new javax.swing.JTextField();

        setClosable(true);
        setTitle("Registrar Hospedaje");

        btnGrabaHospedje.setText("Grabar");
        btnGrabaHospedje.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGrabaHospedjeActionPerformed(evt);
            }
        });

        btnCancelaHospedaje.setText("Cancelar");
        btnCancelaHospedaje.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelaHospedajeActionPerformed(evt);
            }
        });

        jLabel1.setText("De la Reserva:");

        jLabel2.setText("Fecha");

        jLabel3.setText("Cliente");

        jLabel4.setText("Tipo de Habitacion");

        jLabel5.setText("Dias de reserva");

        lblFecha.setText("XXXXXXX XXXXXXX XXXXXX xXXXX");

        lblCliente.setText("XXXXXXX XXXXXXX XXXXXX xXXXX");

        lbldiasreserva.setText("XXXXXXX XXXXXXX XXXXXX xXXXX");

        cbTipoHabitacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbTipoHabitacion.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbTipoHabitacionItemStateChanged(evt);
            }
        });

        jLabel9.setText("Habitacion");

        cbHabitacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel10.setText("Fecha");

        jLabel11.setText("Dias");

        txtFecha.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(btnGrabaHospedje)
                        .addGap(40, 40, 40)
                        .addComponent(btnCancelaHospedaje))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbHabitacion, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbldiasreserva, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbTipoHabitacion, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtdias, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtFecha, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lblCliente))
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lbldiasreserva))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbTipoHabitacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(cbHabitacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtdias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGrabaHospedje)
                    .addComponent(btnCancelaHospedaje))
                .addContainerGap(41, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelaHospedajeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelaHospedajeActionPerformed
       // TODO add your handling code here:
       HospedajeLista form = new HospedajeLista();
       form.setEscritorio(escritorio);
       escritorio.add(form);
       form.preLoadFormHospLst();
       form.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_btnCancelaHospedajeActionPerformed

    private void btnGrabaHospedjeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGrabaHospedjeActionPerformed
       try {
            hospedaje = new  Hospedaje();
			hospedaje.setFecAlquiler(Utiles.stringToDate(txtFecha.getText(), Utiles.FORMATO_FECHA_CORTA));
			hospedaje.setDiasreserva(Integer.parseInt(txtdias.getText()));
			Habitacion hab = listaHabitaciones.get(cbHabitacion.getSelectedIndex());
			hab.setEstado(2);
			hospedaje.setHabitacion(hab);
			hospedaje.setEstado(2);
			Users ulogin = Login.getUserLogin();
            hospedaje.setUsersByIdUsuarioCli(reserva.getUsersByIdUsuarioCli());
            hospedaje.setUsersByIdUsuarioEmp(ulogin);
			Hospedaje rsertmp = hospedajeService.obtenerReservaExacta(txtFecha.getText(),hab.getIdhabitacion().toString());
            if( rsertmp != null){
               if(!rsertmp.getUsersByIdUsuarioCli().getUsername().equals(hospedaje.getUsersByIdUsuarioCli().getUsername())){
                  JOptionPane.showMessageDialog(this, "Ya esta reservada por otro cliente en esa fecha. No se puede registrar su hospedaje");
                  return;
               }
            }
            if( hospedajeService.obtenerHospedajeExacto(txtFecha.getText(),hab.getIdhabitacion().toString()) != null){
               JOptionPane.showMessageDialog(this, "Esta habitacion ya se encuentra Habitada");
               return;
            }
			hospedajeService.registrarHospedaje(hospedaje);
			hospedaje = hospedajeService.obtenerHospedaje(hospedaje.getIdhospedaje());
			
		} catch (Exception e) {
            e.printStackTrace();
            return;
        }

       HospedajeLista form = new HospedajeLista();
       form.setEscritorio(escritorio);
       escritorio.add(form);
       form.preLoadFormHospLst();
       form.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_btnGrabaHospedjeActionPerformed

    private void cbTipoHabitacionItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbTipoHabitacionItemStateChanged
       if(cbTipoHabitacion.getSelectedIndex() != -1){
          int tip = cbTipoHabitacion.getSelectedIndex();
          Habitacion hab = new Habitacion();
          hab.setEstado(0);
          hab.setTipohabitacion(habitacionService.listaTipoHabitacion().get(tip));
          cbHabitacion.removeAllItems();
          listaHabitaciones = habitacionService.listaHabitacionessinEstado(hab);
          System.out.println("ELEMENTOS = "+listaHabitaciones.size());
          for(Habitacion habitacion : listaHabitaciones){
             cbHabitacion.addItem(habitacion.getNumero());
          }
       }
    }//GEN-LAST:event_cbTipoHabitacionItemStateChanged



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelaHospedaje;
    private javax.swing.JButton btnGrabaHospedje;
    private javax.swing.JComboBox cbHabitacion;
    private javax.swing.JComboBox cbTipoHabitacion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lblCliente;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JLabel lbldiasreserva;
    private javax.swing.JTextField txtFecha;
    private javax.swing.JTextField txtdias;
    // End of variables declaration//GEN-END:variables

   public void cargaInicial(){
      try {
         if(hospedajeService==null){hospedajeService = (HospedajeService) Inicial.getFactory().getBean("hospedajeService");}
         if(usuarioService==null){usuarioService= (UsuarioService) Inicial.getFactory().getBean("usuarioService");}
         if(habitacionService==null){habitacionService = (HabitacionService)Inicial.getFactory().getBean("habitacionService");}
         lblFecha.setText(Utiles.dateToString(reserva.getFecReserva(),"dd/MM/yyyy"));
         lblCliente.setText(reserva.getUsersByIdUsuarioCli().getNombrecompleto());
         lbldiasreserva.setText(reserva.getDiasreserva().toString());
         txtFecha.setText(Utiles.dateToString(new Date(), "dd/MM/yyyy"));
         txtdias.setText(reserva.getDiasreserva().toString());
         cbTipoHabitacion.removeAllItems();
         for(Tipohabitacion th : habitacionService.listaTipoHabitacion()){
            cbTipoHabitacion.addItem(th.getNombre());
         }
         if(reserva.getHabitacion() != null ){
            cbTipoHabitacion.setSelectedItem(reserva.getHabitacion().getNumero());
            if(cbTipoHabitacion.getSelectedIndex() != -1){
             Habitacion hab = new Habitacion();
             hab.setTipohabitacion(reserva.getHabitacion().getTipohabitacion());
             cbHabitacion.removeAllItems();
             listaHabitaciones = habitacionService.listaHabitacionessinEstado(hab);
             System.out.println("ELEMENTOS tipo ha = "+listaHabitaciones.size());
             for(Habitacion habitacion : listaHabitaciones){
                cbHabitacion.addItem(habitacion.getNumero());
             }
             cbHabitacion.setSelectedItem(reserva.getHabitacion().getNumero());
            }
         }
      } catch (Exception e) {
         System.out.println(e.getMessage());
      }
      
   }

   

}
